package com.mpcm.web.client.core;

import com.gwtplatform.mvp.client.ViewWithUiHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.animation.client.Animation;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ScrollEvent;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.mpcm.web.shared.SharedConstants;

public class HomeLayoutView extends ViewWithUiHandlers<HomeLayoutUiHandlers> implements
		HomeLayoutPresenter.MyView {

	private final Widget widget;

	public interface Binder extends UiBinder<Widget, HomeLayoutView> {
	}

	private class ScrollPageAnimation extends Animation {
		@Override
		protected void onUpdate(double progress) {
			scrollPanelBase.setVerticalScrollPosition((int)(progress * 1000));
		}
	}
	
	private Timer posTimer;
	private ScrollPageAnimation scrollAnim;
	
	@UiField ScrollPanel scrollPanelBase;
	@UiField SimplePanel homeContainer;
	@UiField SimplePanel caseContainer;
	@UiField SimplePanel serviceContainer;
	@UiField SimplePanel aboutContainer;
	@UiField SimplePanel contactContainer;
	@UiField HorizontalPanel headerContainer;
	@UiField Hyperlink hyperlinkHome;
	@UiField Hyperlink hyperlinkCase;
	@UiField Hyperlink hyperlinkService;
	@UiField Hyperlink hyperlinkAbout;
	@UiField Hyperlink hyperlinkContact;
	
	@Inject
	public HomeLayoutView(final Binder binder) {
		widget = binder.createAndBindUi(this);
		
		Window.addResizeHandler(new ResizeHandler(){
			@Override
			public void onResize(ResizeEvent event) {
				resetHeaderPosition();
				
				if(getUiHandlers() != null) {
					getUiHandlers().onSetClientWidth();
					getUiHandlers().onBaseScroll();
				}
			}}
		);
		
		posTimer = new Timer() {
			public void run() {
				if(getUiHandlers() != null)
					getUiHandlers().onBaseScroll();
			}
	    };
	    posTimer.scheduleRepeating(SharedConstants.SCHEDULE_PERIOD);
	    
	    scrollAnim = new ScrollPageAnimation();
	    
	    hyperlinkHome.addDomHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				scrollAnim.run(3000);
			}}, ClickEvent.getType());
	}
	
	@Override
	public void setInSlot(Object slot, Widget content) {
		if(slot == HomeLayoutPresenter.TYPE_RevealMainContent1) {
			if(content != null)
				homeContainer.add(content);
		} else if (slot == HomeLayoutPresenter.TYPE_RevealMainContent2) {
			if(content != null)
				aboutContainer.add(content);
		} else if (slot == HomeLayoutPresenter.TYPE_RevealMainContent3) {
			if(content != null)
				serviceContainer.add(content);
		} else if (slot == HomeLayoutPresenter.TYPE_RevealMainContent4) {
			if(content != null)
				caseContainer.add(content);
		} else if (slot == HomeLayoutPresenter.TYPE_RevealMainContent5) {
			if(content != null)
				contactContainer.add(content);
		} else
			super.setInSlot(slot, content);
	}

	@Override
	public Widget asWidget() {
		return widget;
	}

	@Override
	public void resetScrollPanel() {
		int width = Window.getClientWidth();
		int height = Window.getClientHeight();
		this.scrollPanelBase.setSize(width + "px", height + "px");
	}
	
	@UiHandler("scrollPanelBase")
	void onScrollPanelBaseScroll(ScrollEvent event) {
		//if(getUiHandlers() != null)
			//getUiHandlers().onBaseScroll();
	}

	@Override
	public void resetHeaderPosition() {
		int clientWidth = Window.getClientWidth();
		int headerLeft = 0;
		if(clientWidth > headerContainer.getElement().getClientWidth())
			headerLeft = (clientWidth - headerContainer.getElement().getClientWidth()) / 2;
		
		this.headerContainer.getElement().getStyle().setLeft(headerLeft, Unit.PX);
	}
}
